//
//  NicknameSetViewController.m
//  iOS-CtrlV
//
//  Created by 龚 杰 on 12-12-21.
//  Copyright (c) 2012年 Wang Li. All rights reserved.
//

#import "NicknameSetViewController.h"
#import <QuartzCore/QuartzCore.h>


@interface NicknameSetViewController ()

@end

@implementation NicknameSetViewController
@synthesize delegate;
@synthesize nicktext;

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

//- (id)initData:(NSString*)str
//{
//    if (self = [super init]) {
//        
//        m_text = str;
//    }
//    return self;
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        [self.navigationController.navigationBar setBackgroundImage:tAppDel.m_MidNavImg forBarMetrics: UIBarMetricsDefault];
        self.navigationController.navigationBar.clipsToBounds = NO;
        
//        UIImageView* tImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navshadow.png"]];
//        tImgView.frame =CGRectMake(0, 44, 320, 5);
//        [self.navigationController.navigationBar addSubview:tImgView];
//        [tImgView release];
    }
    m_BackBttBarItem =[UIButton buttonWithType:UIButtonTypeCustom];
    m_BackBttBarItem.frame =CGRectMake(0, 0, 44, 44);
    [m_BackBttBarItem setImage:[UIImage imageNamed:@"navclose.png"] forState:UIControlStateNormal];
    [m_BackBttBarItem addTarget:self action:@selector(navBackBtt) forControlEvents:UIControlEventTouchUpInside];
    [m_BackBttBarItem retain];
    [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    
    m_settngBttBarItem =[UIButton buttonWithType:UIButtonTypeCustom];
    m_settngBttBarItem.frame =CGRectMake(self.navigationController.navigationBar.frame.size.width-44, 0, 44, 44);
    [m_settngBttBarItem setImage:[UIImage imageNamed:@"navyes.png"] forState:UIControlStateNormal];
    [m_settngBttBarItem addTarget:self action:@selector(navSettingBtt) forControlEvents:UIControlEventTouchUpInside];
    [m_settngBttBarItem retain];
    [self.navigationController.navigationBar addSubview:m_settngBttBarItem];
    
    
    if (_nsicknameView == nil) {
        _nsicknameView = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width-40,120)];
        [_nsicknameView setBackgroundColor:[UIColor whiteColor]];
        _nsicknameView.layer.borderColor  = [UIColor colorWithRed:187/250.f green:187/255.0f blue:187/255.0f alpha:1.0].CGColor;
        _nsicknameView.layer.borderWidth = 1.0f;
        _nsicknameView.layer.masksToBounds=YES;
        _nsicknameView.layer.cornerRadius=10;
        _nsicknameView.returnKeyType =UIReturnKeyDone;
        _nsicknameView.keyboardType = UIKeyboardTypeNamePhonePad;
        _nsicknameView.delegate = self;
        _nsicknameView.keyboardAppearance = UIKeyboardAppearanceDefault;
        _nsicknameView.font = [UIFont systemFontOfSize:16.0];
    }

    _nsicknameView.text = self.nicktext;
    [self.view addSubview:_nsicknameView];
   
}
- (void)viewWillDisappear:(BOOL)animated
{
        [m_settngBttBarItem removeFromSuperview];
        [m_BackBttBarItem removeFromSuperview];
}

- (void)navBackBtt
{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)navSettingBtt
{
    if (delegate !=nil) {
        [delegate GetText:_nsicknameView.text];
    }
     [self.navigationController popViewControllerAnimated:YES];
}
#pragma make-
#pragma UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"dsafasdf1111");

}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSLog(@"dsafasdf2222");

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSCharacterSet *doneButtonCharacterSet = [NSCharacterSet newlineCharacterSet];
    
    NSRange replacementTextRange = [text rangeOfCharacterFromSet:doneButtonCharacterSet];
    
    NSUInteger location = replacementTextRange.location;

    
     if (location != NSNotFound){
        
        [textView resignFirstResponder];
         [self navSettingBtt];
        
        return NO; 
    }
 return YES;
}
- (void)textViewDidChangeSelection:(UITextView *)textView
{
    NSLog(@"dsafasdf44444");
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [m_BackBttBarItem release];
    [m_settngBttBarItem release];
    [_nsicknameView resignFirstResponder];
    [_nsicknameView release];
    self.nicktext = nil;
    [super dealloc];
}
@end
